home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 28
/
Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso
/
Aminet
/
gfx
/
misc
/
MesaGL-aux.lha
/
src
/
shapes.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-09-12
|
28KB
|
1,206 lines
/*
* shapes.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "GL/gl.h"
#include "GL/glu.h"
#include "glaux.h"
#include "3d.h"
#define SPHEREWIRE 0
#define CUBEWIRE 1
#define BOXWIRE 2
#define TORUSWIRE 3
#define CYLINDERWIRE 4
#define ICOSAWIRE 5
#define OCTAWIRE 6
#define TETRAWIRE 7
#define DODECAWIRE 8
#define CONEWIRE 9
#define SPHERESOLID 10
#define CUBESOLID 11
#define BOXSOLID 12
#define TORUSSOLID 13
#define CYLINDERSOLID 14
#define ICOSASOLID 15
#define OCTASOLID 16
#define TETRASOLID 17
#define DODECASOLID 18
#define CONESOLID 19
#ifndef PI
#define PI 3.1415926535897
#endif
/*
* structure for each geometric object
*/
typedef struct model {
GLuint list; /*
* display list to render object
*/
struct model *ptr; /*
* pointer to next object
*/
int numParam; /*
* # of parameters
*/
GLdouble *params; /*
* array with parameters
*/
} MODEL, *MODELPTR;
/*
* array of linked lists--used to keep track of display lists
* * for each different type of geometric object.
*/
static MODELPTR lists[25] =
{
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
};
GLuint findList(int lindex, GLdouble * paramArray, int size);
int compareParams(GLdouble * oneArray, GLdouble * twoArray, int size);
GLuint makeModelPtr(int lindex, GLdouble * sizeArray, int count);
static void drawbox(GLdouble, GLdouble, GLdouble,
GLdouble, GLdouble, GLdouble, GLenum);
static void doughnut(GLdouble, GLdouble, GLint, GLint, GLenum);
static void icosahedron(GLdouble *, GLdouble, GLenum);
static void octahedron(GLdouble *, GLdouble, GLenum);
static void tetrahedron(GLdouble *, GLdouble, GLenum);
static void subdivide(int, GLdouble *, GLdouble *, GLdouble *,
GLdouble *, GLdouble, GLenum, int);
static void drawtriangle(int, int, int,
GLdouble *, GLdouble, GLenum, int);
static void recorditem(GLdouble *, GLdouble *, GLdouble *,
GLdouble *, GLdouble, GLenum, int);
static void initdodec(void);
static void dodecahedron(GLdouble *, GLdouble, GLenum);
static void pentagon(int, int, int, int, int, GLenum);
/*
* Render wire frame or solid sphere. If no display list with
* * the current model size exists, create a new display list.
*/
void auxWireSphere(GLdouble radius)
{
GLUquadricObj *quadObj;
GLdouble *sizeArray;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 1);
*sizeArray = radius;
displayList = findList(SPHEREWIRE, sizeArray, 1);
if (displayList == 0) {
glNewList(makeModelPtr(SPHEREWIRE, sizeArray, 1),
GL_COMPILE_AND_EXECUTE);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_LINE);
gluSphere(quadObj, radius, 16, 16);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
void auxSolidSphere(GLdouble radius)
{
GLUquadricObj *quadObj;
GLdouble *sizeArray;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 1);
*sizeArray = radius;
displayList = findList(SPHERESOLID, sizeArray, 1);
if (displayList == 0) {
glNewList(makeModelPtr(SPHERESOLID, sizeArray, 1),
GL_COMPILE_AND_EXECUTE);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluSphere(quadObj, radius, 16, 16);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
/*
* Render wire frame or solid cube. If no display list with
* * the current model size exists, create a new display list.
*/
void auxWireCube(GLdouble size)
{
GLdouble *sizeArray;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 1);
*sizeArray = size;
displayList = findList(CUBEWIRE, sizeArray, 1);
if (displayList == 0) {
glNewList(makeModelPtr(CUBEWIRE, sizeArray, 1),
GL_COMPILE_AND_EXECUTE);
drawbox(-size / 2., size / 2., -size / 2., size / 2.,
-size / 2., size / 2., GL_LINE_LOOP);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
void auxSolidCube(GLdouble size)
{
GLdouble *sizeArray;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 1);
*sizeArray = size;
displayList = findList(CUBESOLID, sizeArray, 1);
if (displayList == 0) {
glNewList(makeModelPtr(CUBESOLID, sizeArray, 1),
GL_COMPILE_AND_EXECUTE);
drawbox(-size / 2., size / 2., -size / 2., size / 2.,
-size / 2., size / 2., GL_QUADS);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
/*
* Render wire frame or solid cube. If no display list with
* * the current model size exists, create a new display list.
*/
void auxWireBox(GLdouble width, GLdouble height, GLdouble depth)
{
GLdouble *sizeArray, *tmp;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 3);
tmp = sizeArray;
*tmp++ = width;
*tmp++ = height;
*tmp++ = depth;
displayList = findList(BOXWIRE, sizeArray, 3);
if (displayList == 0) {
glNewList(makeModelPtr(BOXWIRE, sizeArray, 3),
GL_COMPILE_AND_EXECUTE);
drawbox(-width / 2., width / 2., -height / 2., height / 2.,
-depth / 2., depth / 2., GL_LINE_LOOP);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
void auxSolidBox(GLdouble width, GLdouble height, GLdouble depth)
{
GLdouble *sizeArray, *tmp;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 3);
tmp = sizeArray;
*tmp++ = width;
*tmp++ = height;
*tmp++ = depth;
displayList = findList(BOXSOLID, sizeArray, 3);
if (displayList == 0) {
glNewList(makeModelPtr(BOXSOLID, sizeArray, 3),
GL_COMPILE_AND_EXECUTE);
drawbox(-width / 2., width / 2., -height / 2., height / 2.,
-depth / 2., depth / 2., GL_QUADS);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
/*
* Render wire frame or solid tori. If no display list with
* * the current model size exists, create a new display list.
*/
void auxWireTorus(GLdouble innerRadius, GLdouble outerRadius)
{
GLdouble *sizeArray, *tmp;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 2);
tmp = sizeArray;
*tmp++ = innerRadius;
*tmp++ = outerRadius;
displayList = findList(TORUSWIRE, sizeArray, 2);
if (displayList == 0) {
glNewList(makeModelPtr(TORUSWIRE, sizeArray, 2),
GL_COMPILE_AND_EXECUTE);
doughnut(innerRadius, outerRadius, 5, 10, GL_LINE_LOOP);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
void auxSolidTorus(GLdouble innerRadius, GLdouble outerRadius)
{
GLdouble *sizeArray, *tmp;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 2);
tmp = sizeArray;
*tmp++ = innerRadius;
*tmp++ = outerRadius;
displayList = findList(TORUSSOLID, sizeArray, 2);
if (displayList == 0) {
glNewList(makeModelPtr(TORUSSOLID, sizeArray, 2),
GL_COMPILE_AND_EXECUTE);
doughnut(innerRadius, outerRadius, 8, 15, GL_QUADS);
glEndList();
}
else {
glCallList(displayList);
free(sizeArray);
}
}
/*
* Render wire frame or solid cylinders. If no display list with
* * the current model size exists, create a new display list.
*/
void auxWireCylinder(GLdouble radius, GLdouble height)
{
GLUquadricObj *quadObj;
GLdouble *sizeArray, *tmp;
GLuint displayList;
sizeArray = (GLdouble *) malloc(sizeof(GLdouble) * 2);
tmp = sizeArray;
*tmp++ = radius;
*tmp++ = height;
displayList = findList(CYLINDERWIRE, sizeArray, 2);
if (displayList == 0) {
glNewList(makeModelPtr(CYLINDERWIRE, sizeArray, 2),
GL_COMPILE_AND_EXECUTE);
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, -1.0);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_LINE);
gluCylinder(quadObj, radius, radius, height, 12, 2);